package com.longteng.lesson.interceptor;

import com.longteng.lesson.po.MyUser;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by Administrator
 * DATE : 2018/5/20
 * TIME : 16:25
 * PROJECT : java-lesson
 * PACKAGE : com.longteng.lesson
 *
 * @author <a href="mailto:taki@qq.com">Administrator</a>
 */
public class MyInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //在调用controller之前会执行这个拦截器的方法
        //当返回false的时候代表终止用户的请求，什么都不做
        HttpSession httpSession = request.getSession();
        System.out.println("session过期时长:"+httpSession.getMaxInactiveInterval());
        System.out.println(httpSession.getId());
        System.out.println();
        MyUser myUser = (MyUser) httpSession.getAttribute("loginUser");
//        if(myUser==null){
//            如果等于null 说明他没有登录，这里做重定向跳转
//            response.sendRedirect("/checkParamPage");
//        }
        return true;
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//        System.out.println(">>>MyInterceptor>>>>>>>请求处理之后进行调用，但是在视图被渲染之前（Controller方法调用之后）");
    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//        System.out.println(">>>MyInterceptor2>>>>>>>在整个请求结束之后被调用，也就是在DispatcherServlet 渲染了对应的视图之后执行（主要是用于进行资源清理工作）");
    }
}
